package cc.ufinity.networkdiagnose.widget;
 
import java.lang.reflect.Field;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ExpandableListView;
import cc.ufinity.networkdiagnose.adapter.ResultExpandableListAdapter;
import cc.ufinity.networkdiagnose.app.AppLogs;
import cc.ufinity.networkdiagnose.app.GlobalApp;
import cc.ufinity.networkdiagnose.manager.CreateResultXML;
import cc.ufinity.networkdiagnose.manager.DBManager;
import cc.ufinity.networkdiagnose.manager.GroupResult;
import cc.ufinity.networkdiagnose.manager.MessageWhat;
import cc.ufinity.networkdiagnose.manager.RandomGUID;
import cc.ufinity.networkdiagnose.manager.Result;
import cc.ufinity.networkdiagnose.manager.Task;
import cc.ufinity.networkdiagnose.manager.WlanManager;
import cc.ufinity.networkdiagnose.util.StringUtils;
import cc.ufinity.networkdiagnose.view.R;

@SuppressLint("ViewConstructor")
public class TaskTestResultAll extends GeneralTestWidget {

	private WlanManager wlanManager;

    private String cameraFlag= "";

//	private ActivityBroadcastReceiver myReceiver;
    private Activity ctx;
    private Task task;
   // private Button backBtn;
    private Button btnDraw;
   // private LinearLayout taskNumLayout;
    private Handler handler;
    private String fileName;
    private Button btnPic;
    private Thread thread;
	private boolean connected = false;
	//private boolean connected2 = false;
//    private Button uploadButton;
	
	private long creatTime;
	private List<String> fileNames;
	
	private ExpandableListView resultListView;
	private ResultExpandableListAdapter listAdapter;
	private List<GroupResult> groupResultlist;
	
    public TaskTestResultAll(Activity context) {
        super(context);
        inflate(context, R.layout.task_result_widget_all, this);
        this.ctx = context;
        wlanManager = new WlanManager(ctx);
        
        fileNames = new ArrayList<String>();
        
        task = (Task) ctx.getIntent().getSerializableExtra(TAG_REFER_TASK); 
        cameraFlag =(String)ctx.getIntent().getSerializableExtra(TAG_CAMERA_FLAG); 
        ctx.getIntent().putExtra(TAG_CAMERA_TASK, task);
        SharedPreferences settings = ctx.getSharedPreferences(MyMobileCfgWidget.PREFS_NAME, 0);
        String tel = settings.getString(MyMobileCfgWidget.PREFS_KEY, "");
        task.testPhone = tel;
        
        /*initTask(task,ctx,task.selNetType);
        task = dealWebAndPeapData(task);*/
        
        boolean tempResult = false;
        if(task.selNetType == ConnectivityManager.TYPE_WIFI)
        {
        	 for(int i=0;i<task.testedAp.size();i++){
               	if (task.scanResult !=null &&task.scanResult.BSSID!=null &&task.testedAp.get(i).equals(task.scanResult.BSSID)) {
               		tempResult = true;
       			}
             }
             if(! tempResult)
               	task.testedAp.add(task.scanResult.BSSID);
        }
        tempResult = false; 
        int tempNum = 0;
        for(int i=0;i< GlobalApp.taskTestList.size();i++){
        	if ( GlobalApp.taskTestList.get(i).id == task.id) {
        		tempResult = true;
        		break;
			}
        	tempNum ++;
        } 
        if(! tempResult)
        	GlobalApp.taskTestList.add(task);
        else  
        	GlobalApp.taskTestList.set(tempNum, task); 
        
  //      taskNumLayout = (LinearLayout) this.findViewById(R.id.taskNumLayout);
        //backBtn = (Button) this.findViewById(R.id.task_result_btnBack_all);
        btnDraw = (Button) this.findViewById(R.id.btn_draw_all);
        btnPic = (Button)this.findViewById(R.id.btnpic_all);
        
        if (GlobalApp.DEBUG) {
			System.out.println("tasktestresultall 测试类型" + GlobalApp.getTestType());
		}
        if (GlobalApp.getTestType() != 1) {
			btnPic.setVisibility(View.VISIBLE);
		}else {
			btnPic.setVisibility(View.GONE);
		}
   
        btnPic.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            	GlobalApp.who_to_CameraWidget = MessageWhat.TaskTestResultAll;
                CameraWidget cw = new CameraWidget(ctx);
               try {
            	   ctx.setContentView(cw);
				} catch (Exception e) {
					// TODO: handle exception
					Log.e("CameraShow", e.getMessage());
				}
            }
        });
        btnDraw.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != fileNames && fileNames.size()>0) {
                    showMessage(ctx, "正在重新上传中...");
                    if (!AppLogs.isUpload && !AppLogs.logData.equals("")) {
                    	 new AppLogs(ctx);//上传指标异常日志
					}
                    new Thread() {
                        public void run() { 
                       	int num = uplodFiles(fileNames, ctx, handler);
                           for(int i=0;i<num;i++)
                            {
                    	   		fileNames.remove(0); 
                           }
                        }
                    }.start();
                } else {
                    showMessage(ctx, "文件已上传或未进行测试.");
                }
               //  shotScreen();
            }   
        });
        handler = new Handler() {
            public void handleMessage(Message msg) {
                switch (msg.what) {
                case MessageWhat.WIFI_SUCESS_CONNECTED:  
					dismissMessageDialog();
                	upload();
                    break;
                case MessageWhat.FTP_ACCOUNT_INVALID:
                    showMessage(ctx, "无法获取FTP账号");
                    break;
                case MessageWhat.FTP_UPLOAD_TASK_DONE:
                	if(cameraFlag != null && cameraFlag.equals(TAG_CAMERA_FLAG_VALUE)) {
                		System.out.println("--------------  说好的拍照呢 ？ 上传成功 。 。 。  ---------------");
                		
                	} else {
                		showMessage(ctx, "上传成功.");
	                    int value = ctx.getIntent().getIntExtra(MessageWhat.INDEX_TaskResultAll_WIDGET2, 0);
	            		if (value == 1) {
	            			
	            			//网络测试不提示拍照
	            			if (GlobalApp.getTestType() !=1 ) {
	            				showCameraDialog(ctx);
							}
	            			
	            		}
                	}
            			
                    break;
                case MessageWhat.FTP_UPLOAD_FAILED:
                    showMessage(ctx, "上传失败");
                    break;
                case MessageWhat.FTP_LOGIN_FAILED:
                    showMessage(ctx, "FTP登录失败");
                    break;
                }
            }
        }; 
        wlanManager.setHandler(handler); 
        showResult();
        cameraFlag="00ooo";
        if(cameraFlag == null || !cameraFlag.equals(TAG_CAMERA_FLAG_VALUE))    {
        	System.err.println("---开始创建文件----------");
        	connectNetWork();  
        }
		 
        ctx.getIntent().putExtra(TAG_REFER_RESULT_FLAG, TAG_REFER_RESULTALL2);
    }
    
    private void showResult() {
    	
    	resultListView = (ExpandableListView)findViewById(R.id.resultAllList);
    	resultListView.setGroupIndicator(null);
		
    	resultListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
			
			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				//被点中的result
				Result result = (Result)parent.getExpandableListAdapter().getChild(groupPosition, childPosition);
				System.out.println(result.getName() + " : " + result.getInfo());
				
				return true;
			}
		});
		
    	show();
		
		listAdapter = new ResultExpandableListAdapter(ctx, groupResultlist);
		resultListView.setAdapter(listAdapter);
		resultListView.expandGroup(0);
    }

    private void connectNetWork(){
		   showMessageDialog(ctx, "正在连接网络..."); 
		   thread = new Thread() { 
		        @Override
		        public void run() {
		        	 if(wlanManager.checkNetworkType() != 1 && wlanManager.checkNetworkType() != 5){
		        		 wlanManager.closeWifi();
		        		 wlanManager.setMobileDataEnabled(true);
		        		 while(!connected) {
		        			 if(wlanManager.checkNetworkType() == 5){
				                	connected = true;
				                	wlanManager.sendMsg(MessageWhat.WIFI_SUCESS_CONNECTED);
				                }
		        		 }
		        	 } else {
		        		 wlanManager.sendMsg(MessageWhat.WIFI_SUCESS_CONNECTED);
		        	 }
		        }  
		    };

	    	thread.start(); 
	    }
  
    private void upload() {
//    	if(task.scanResult != null && task.scanResult.SSID.equals(WlanManager.SSID_CMCC)){
//			WebAuthManager wManager = new WebAuthManager(ctx);
//			String  defaultUrl = ctx.getResources().getString(R.string.default_url);
//	        DBManager db = new DBManager(ctx);
//			Account config = db.getAccount();
//			String userName ="",pwd="";
//			if(null != config){ 
//				userName = config.userName;
//				pwd = config.password;
//			}
//			wManager.auth(defaultUrl, userName, pwd);
//		 	db.release();
//		}
//    	
    	if(cameraFlag == null || !cameraFlag.equals(TAG_CAMERA_FLAG_VALUE)) {
//    		 createResultXML();
    		 creatTime=System.currentTimeMillis();
    		 createResultXML(GlobalApp.taskWlan, 1);
    		 fileName = new CreateResultXML(creatTime).CreateNeighborCellxml(GlobalApp.taskWlan, ctx);
         	 if(fileName != null) {
         		 fileNames.add(fileName);
         	 }
    		 createResultXML(GlobalApp.taskTD, 0);
    		 try {
					Thread.sleep(10);
			 } catch (InterruptedException e) {
			 }
    		 createResultXML(GlobalApp.taskGSM, 0); 
			 for(Task t:GlobalApp.taskOtherList) {
				 try {
						Thread.sleep(10);
				 } catch (InterruptedException e) {
				 }
	    		 createResultXML(t, 1);
			 }
    		 ctx.getIntent().putExtra("xml", fileName); 
	         DBManager db = new DBManager(ctx);
	         db.saveTask(GlobalApp.taskWlan.mac, GlobalApp.taskWlan.apName, String.valueOf(GlobalApp.taskWlan.id));
	         db.release();
 System.err.println(GlobalApp.taskWlan.id+"\n"+GlobalApp.taskTD.id+"\n"+GlobalApp.taskGSM.id);
 for(Task task:GlobalApp.taskOtherList) {
	 System.err.println(task.id);
 }
    		 GlobalApp.GUID = (new RandomGUID()).valueAfterMD5;
    		 System.err.println("已生成文件：" ); 
			 if (null != fileNames) {
                 showMessage(ctx, "正在上传中...");
                 
                 if (!AppLogs.isUpload && !AppLogs.logData.equals("")) {
                	 new AppLogs(ctx);//上传指标异常日志
				}
                 
                 new Thread() {
                     public void run() { 
                    	int num = uplodFiles(fileNames, ctx, handler);
                        System.out.println(fileNames.size()+"==================="+num);
                        for(int i=0;i<num;i++)
                         {
                 	   		fileNames.remove(0); 
                        }
                        System.out.println(fileNames.size()+"===================");
                     }
                 }.start();
             } else {
                 showMessage(ctx, "文件已上传或未进行测试.");
             } 
    		
    	} else if(cameraFlag.equals(TAG_CAMERA_FLAG_VALUE)) {
//    		final String jpg_name =(String)ctx.getIntent().getSerializableExtra("jpg_name"); 
//    		if (null != jpg_name) {
//                showMessage(ctx, "正在上传截屏...");
//                /*new Thread() {
//                    public void run() {
//                        uploadPic(jpg_name, ctx, handler);
//                    }
//                }.start();*/
//            }
    		System.out.println("--------------  说好的拍照呢 ？  ---------------");
    	}
    		
    }
    
    /**
     * 
     * @param task
     * @param flag:1 WLAN   0 TD或GSM
     */
    private void createResultXML(Task task, int flag) {
    	//GlobalApp.DEBUG=false;
    	String fileName = "";
    	if(flag == 1) {
    		fileName = new CreateResultXML(creatTime).createWLANxml(task, ctx);
        	if(fileName != null) {
        		fileNames.add(fileName);
        	}
    		/*fileName = new CreateResultXML().CreateNeighborCellxml(task, ctx);
        	if(fileName != null) {
        		fileNames.add(fileName);
        	}*/
    	} else {
    		fileName = new CreateResultXML(creatTime).CreateGSM_TDxml(task, ctx);    	
    		if(fileName != null) {
        		fileNames.add(fileName);
        	}
    	}
    	//GlobalApp.DEBUG=true;
    }

   /* private void createResultXML() {
        task.endTime = StringUtils.getDateTime();
        TaskParser parser = new PullTaskParser();
        if(task.selNetType == ConnectivityManager.TYPE_WIFI){
        	fileName=parser.CreateWLANxml(task,ctx);
        }else if(task.selNetType ==ConnectivityManager.TYPE_MOBILE){
        	fileName=parser.CreateGSMxml(task,ctx);
        }else if(task.selNetType ==ConnectivityManager.TYPE_MOBILE_MMS){
        	fileName=parser.CreateTDxml(task,ctx);
        }else {
			
		}
        
    //    fileName = parser.XmlFileCreator(task);
        ctx.getIntent().putExtra("xml", fileName);
        DBManager db = new DBManager(ctx);
        db.saveTask(task.mac, task.apName, String.valueOf(task.id));
        db.release();
        if (null != fileName) {
            showMessage(ctx, "正在上传测试结果...");
            new Thread() {
                public void run() {
                    if (uplod(fileName, ctx, handler))
                        fileName = null;
                }
            }.start();
        }
        ctx.getIntent().removeExtra(TAG_REFER_TASK);
    }*/
    
    private void show() {
        DecimalFormat df = new DecimalFormat("#0.000");
        StringBuffer s = new StringBuffer();  
        float m , n;
        
 	   groupResultlist = new ArrayList<GroupResult>();
 	   GroupResult group = new GroupResult();
 	   List<Result> list = new ArrayList<Result>();
 	   Result result = new Result();
 	   
    //  Wlan网络测试结果
 	   group.setName("Wlan网络测试结果");
 	   
 	   Task task1 = GlobalApp.taskWlan;
 	   Task task2 = GlobalApp.taskTD;
 	   Task task3 = GlobalApp.taskGSM;
 	   initTask(task1,ctx,ConnectivityManager.TYPE_WIFI);
        task1 = dealWebAndPeapData(task1);
 	   
 	   result.setName("HTTP测试结果");
        s.append("平均时延：").append(df.format(StringUtils.parseFloat(task1.httpAnwerDelay))).append(" s").append('\n');
        s.append("访问成功率：").append(task1.httpCallSuccessRate).append(" %");
 	   result.setInfo(s.toString());
 	   list.add(result);
 	   
 	   result = new Result();
 	   result.setName("FTP测试结果");      
        s = new StringBuffer();
        float tempUpAvg = Float.parseFloat(task1.ftpUpAvgSuccessRate) *8 ;
        float tempDownAvg =Float.parseFloat(task1.ftpDownAvgSuccessRate) *8 ;
        s.append("平均上传速度：").append(tempUpAvg).append(" Kb/s").append('\n');
        s.append("平均下载速度：").append(tempDownAvg).append(" Kb/s").append('\n');
        m = (float) StringUtils.parseFloat(task1.ftpUpCount);
        n = (float) StringUtils.parseFloat(task1.ftpUpSuccessCount);
        s.append("上传成功率：").append(m==0?0:df.format( 100*n / m)).append(" %").append('\n');
        m = (float) StringUtils.parseFloat(task1.ftpDownCount);
        n = (float) StringUtils.parseFloat(task1.ftpDownSuccessCount);
        s.append("下载成功率：").append(m==0?0:df.format( 100*n / m)).append(" %");
 	   result.setInfo(s.toString());
 	   list.add(result);
 	   
 	   result = new Result();
 	   result.setName("PING测试结果");
        s = new StringBuffer();
        s.append("平均时延：").append(df.format((float) StringUtils.parseFloat(task1.pingAvgDelay))).append(" s").append('\n');
        m = (float) StringUtils.parseFloat(task1.pingCount);
        n = (float) StringUtils.parseFloat(task1.pingSuccessCount);
        s.append("丢包率：").append(m==0?0:df.format( 100*(m-n) / m)).append(" %");
        // s.append("Bytes：").append(df.format(avgBytes)).append(" byte;");
 	   result.setInfo(s.toString());
 	   list.add(result);
 	   
 	   result = new Result();
 	   result.setName("AP关联测试结果");
 	   s = new StringBuffer();
        s.append("平均关联时间：").append(task1.relevanceAPAvgTime).append("s").append('\n');
        s.append("关联成功率：").append(task1.relevanceAPSuccessRate).append("%");
 	   result.setInfo(s.toString());
 	   list.add(result);
 	   
 	   result = new Result();
 	   result.setName("WEB认证测试结果");
 	   s = new StringBuffer();
        if(task1!=null && task1.scanResult!=null && task1.scanResult.SSID!=null 
     		   && task1.scanResult.SSID.equals(WlanManager.SSID_CMCC)){
        	   s.append("平均认证时间：").append(task1.webDelay).append("s").append('\n');
            s.append("认证成功率：").append(task1.webSuccessRate).append("%").append('\n');
            s.append("下线成功率：").append(task1.webOffSuccessRate).append("%");
        } else {
        	 	s.append("当前wifi网络类型不支持Web认证\n");
 	   }
 	   result.setInfo(s.toString());
 	   list.add(result);
 	   
 	   result = new Result();
 	   result.setName("PEAP认证测试结果");
 	   s = new StringBuffer();
 	  if(GlobalApp.isPeapTested){
      	   s.append("认证成功率：").append(task1.peapSuccessRate).append("%");
          s.append("\n下线成功率：").append(task1.peapOffSuccessRate).append("%")
          .append("\n平均时延：" + task1.peapDelay + "s");
      } else {
      	   s.append("当前wifi网络类型不支持Peap认证\n");
	   }
 	   result.setInfo(s.toString());
 	   list.add(result);
 	   
 	   result = new Result();
 	   result.setName("隔离测试结果");
 	   s = new StringBuffer();
        s.append("成功率：").append(task1.userIsolateSuccessRate).append("%");
 	   result.setInfo(s.toString());
 	   list.add(result);
 	   
 	   result = new Result();
 	   result.setName("漫游测试结果");
 	   s = new StringBuffer();
        s.append("成功率：").append(task1.userRoamSuccessRate).append("%");
 	   result.setInfo(s.toString());
 	   list.add(result);

 	   result = new Result();
 	   result.setName("信号强度测试结果");
 	   s = new StringBuffer();
//        s.append("CI：").append(task1.ci);
//        s.append("\nLAC：").append(task1.lac);
//        s.append("\n信号强度：").append(task1.rssi).append(" dBm");
 	  s.append("信号强度：").append(task1.rssi).append(" dBm");
 	  s.append("\n经度：" +GlobalApp.longitude);
 	  s.append("\n纬度：" +GlobalApp.latitude);
 	   result.setInfo(s.toString());
 	   list.add(result);
 	   group.setResultList(list);
 	   groupResultlist.add(group);
 	   
    //  TD网络测试结果
 	   initTask(task2, ctx, ConnectivityManager.TYPE_MOBILE_MMS);
 	   
 	   group = new GroupResult();
 	   list = new ArrayList<Result>();
 	   group.setName("TD网络测试结果");
 	   
 	   result = new Result();
 	   result.setName("HTTP测试结果");
        s = new StringBuffer();
        s.append("平均时延：").append(df.format(StringUtils.parseFloat(task2.httpAnwerDelay))).append(" s").append('\n');
        s.append("访问成功率：").append(task2.httpCallSuccessRate).append(" %");
 	   result.setInfo(s.toString());
 	   list.add(result);
 	   
 	   result = new Result();
 	   result.setName("FTP测试结果");
  	   s = new StringBuffer();
  	 float tempUpAvg1 = Float.parseFloat(task2.ftpUpAvgSuccessRate) *8 ;
     float tempDownAvg1 =Float.parseFloat(task2.ftpDownAvgSuccessRate) *8 ;
 	   s.append("平均上传速度：").append(tempUpAvg1).append(" Kb/s").append('\n');
        s.append("平均下载速度：").append(tempDownAvg1).append(" Kb/s").append('\n');
        m = (float) StringUtils.parseFloat(task2.ftpUpCount);
        n = (float) StringUtils.parseFloat(task2.ftpUpSuccessCount);
        s.append("上传成功率：").append(m==0?0:df.format( 100*n / m)).append(" %").append('\n');
        m = (float) StringUtils.parseFloat(task2.ftpDownCount);
        n = (float) StringUtils.parseFloat(task2.ftpDownSuccessCount);
        s.append("下载成功率：").append(m==0?0:df.format( 100*n / m)).append(" %");
 	   result.setInfo(s.toString());
 	   list.add(result);
 	   
 	   result = new Result();
 	   result.setName("PING测试结果");
  	   s = new StringBuffer();
 	   s.append("平均时延：").append(df.format((float) StringUtils.parseFloat(task2.pingAvgDelay))).append(" s").append('\n');
        m = (float) StringUtils.parseFloat(task2.pingCount);
        n = (float) StringUtils.parseFloat(task2.pingSuccessCount);
        s.append("丢包率：").append(m==0?0:df.format( 100*(m-n) / m)).append(" %");
 	   result.setInfo(s.toString());
 	   list.add(result);
 	   
 	   /*result = new Result();
 	   result.setName("信噪比测试结果");
 	   s = new StringBuffer();
        s.append("SNR：").append(task2.snr);
 	   result.setInfo(s.toString());
 	   list.add(result);*/
 	   
 	  result = new Result();
	   result.setName("信号强度测试结果");
	   s = new StringBuffer();
       s.append("CI：").append(GlobalApp.tdCi==0?"":GlobalApp.tdCi );
       s.append("\nLAC：").append(GlobalApp.tdLAc==0?"":GlobalApp.tdLAc);
    //   String rssi = (task2.td == null || task2.td.rssi == null  || task2.td.rssi.equals("0") )?"0":"-"+task2.td.rssi;
       s.append("\n信号强度：").append(GlobalApp.tdRrri==null?" ":GlobalApp.tdRrri).append(" dBm");
	   result.setInfo(s.toString());
	   list.add(result);
	   group.setResultList(list);
	   groupResultlist.add(group);
 	   
 	   
 	//  GSM网络测试结果
 	   initTask(task3, ctx, ConnectivityManager.TYPE_MOBILE);
 	   
 	   group = new GroupResult();
 	   list = new ArrayList<Result>();
 	   group.setName("GSM网络测试结果");
 	   
 	   result = new Result();
 	   result.setName("HTTP测试结果");
        s = new StringBuffer();
        s.append("平均时延：").append(df.format(StringUtils.parseFloat(task3.httpAnwerDelay))).append(" s").append('\n');
        s.append("访问成功率：").append(task3.httpCallSuccessRate).append(" %");
 	   result.setInfo(s.toString());
 	   list.add(result);
 	   
 	   result = new Result();
 	   result.setName("FTP测试结果");
  	   s = new StringBuffer();
  	 float tempUpAvg2 = Float.parseFloat(task3.ftpUpAvgSuccessRate) *8 ;
     float tempDownAvg2 =Float.parseFloat(task3.ftpDownAvgSuccessRate) *8 ;
 	   s.append("平均上传速度：").append(tempUpAvg2).append(" Kb/s").append('\n');
        s.append("平均下载速度：").append(tempDownAvg2).append(" Kb/s").append('\n');
        m = (float) StringUtils.parseFloat(task3.ftpUpCount);
        n = (float) StringUtils.parseFloat(task3.ftpUpSuccessCount);
        s.append("上传成功率：").append(m==0?0:df.format( 100*n / m)).append(" %").append('\n');
        m = (float) StringUtils.parseFloat(task3.ftpDownCount);
        n = (float) StringUtils.parseFloat(task3.ftpDownSuccessCount);
        s.append("下载成功率：").append(m==0?0:df.format( 100*n / m)).append(" %");
 	   result.setInfo(s.toString());
 	   list.add(result);
 	   
 	   result = new Result();
 	   result.setName("PING测试结果");
  	   s = new StringBuffer();
 	   s.append("平均时延：").append(df.format((float) StringUtils.parseFloat(task3.pingAvgDelay))).append(" s").append('\n');
        m = (float) StringUtils.parseFloat(task3.pingCount);
        n = (float) StringUtils.parseFloat(task3.pingSuccessCount);
        s.append("丢包率：").append(m==0?0:df.format( 100*(m-n) / m)).append(" %");
 	   result.setInfo(s.toString());
 	   list.add(result);
 	   
 	   /*result = new Result();
 	   result.setName("信噪比测试结果");
 	   s = new StringBuffer();
        s.append("SNR：").append(task3.snr);
 	   result.setInfo(s.toString());
 	   list.add(result);*/
 	   
 	  result = new Result();
	   result.setName("信号强度测试结果");
	   s = new StringBuffer();
	  s.append("CI：").append(GlobalApp.GsmCi==0?"":GlobalApp.GsmCi );
     s.append("\nLAC：").append(GlobalApp.GsmLAc==0?"":GlobalApp.GsmLAc);
    //   rssi = task3.gsm.rssi.equals("0")?"0":"-"+task3.gsm.rssi;
       s.append("\n信号强度：").append(GlobalApp.gsmTssi==null?"":GlobalApp.gsmTssi).append(" dBm");
	   result.setInfo(s.toString());
	   list.add(result);
	   group.setResultList(list);
	   groupResultlist.add(group);

 //其他网络测试结果
 	   for(Task t:GlobalApp.taskOtherList) {
 		   initTask(t, ctx, ConnectivityManager.TYPE_WIFI);
 		   
 		   group = new GroupResult();
 		   list = new ArrayList<Result>();   
 		  if(t.scanResult.SSID.equals(WlanManager.SSID_ChinaNet)) {
			   group.setName("电信WLAN测试");
			} else if(t.scanResult.SSID.equals(WlanManager.SSID_ChinaUnicom1)
					|| t.scanResult.SSID.equals(WlanManager.SSID_ChinaUnicom2)){
				group.setName("联通WLAN测试");
			} else {
				group.setName("WLAN测试");
			}
 		   
 		   result = new Result();
 		   result.setName("HTTP测试结果");
 	       s = new StringBuffer();
 	       s.append("平均时延：").append(df.format(StringUtils.parseFloat(t.httpAnwerDelay))).append(" s").append('\n');
 	       s.append("访问成功率：").append(t.httpCallSuccessRate).append(" %");
 		   result.setInfo(s.toString());
 		   list.add(result);
 		   
 		   result = new Result();
 		   result.setName("FTP测试结果");
 		   s = new StringBuffer();
 		  float tempUpAvg3 = Float.parseFloat(t.ftpUpAvgSuccessRate) *8 ;
 	     float tempDownAvg3 =Float.parseFloat(t.ftpDownAvgSuccessRate) *8 ;
 	       s.append("平均上传速度：").append(tempUpAvg3).append(" Kb/s").append('\n');
 	       s.append("平均下载速度：").append(tempDownAvg3).append(" Kb/s").append('\n');
 	       m = (float) StringUtils.parseFloat(t.ftpUpCount);
 	       n = (float) StringUtils.parseFloat(t.ftpUpSuccessCount);
 	       s.append("上传成功率：").append(m==0?0:df.format( 100*n / m)).append(" %").append('\n');
 	       m = (float) StringUtils.parseFloat(t.ftpDownCount);
 	       n = (float) StringUtils.parseFloat(t.ftpDownSuccessCount);
 	       s.append("下载成功率：").append(m==0?0:df.format( 100*n / m)).append(" %");
 		   result.setInfo(s.toString());
 		   list.add(result);
 		   
 		   result = new Result();
 		   result.setName("PING测试结果");
 	 	   s = new StringBuffer();
 		   s.append("平均时延：").append(df.format((float) StringUtils.parseFloat(t.pingAvgDelay))).append(" s").append('\n');
 	       m = (float) StringUtils.parseFloat(t.pingCount);
 	       n = (float) StringUtils.parseFloat(t.pingSuccessCount);
 	       s.append("丢包率：").append(m==0?0:df.format( 100*(m-n) / m)).append(" %");
 		   result.setInfo(s.toString());
 		   list.add(result);
 		   
 		   result = new Result();
 		   result.setName("WEB认证测试结果");
 		   s = new StringBuffer();
       	   s.append("平均认证时间：").append(t.webDelay).append("s").append('\n');
           s.append("认证成功率：").append(t.webSuccessRate).append("%").append('\n');
           s.append("下线成功率：").append(t.webOffSuccessRate).append("%");

 		   result.setInfo(s.toString());
 		   list.add(result);
 		   
 		   result = new Result();
 		   result.setName("PEAP认证测试结果");
 		   s = new StringBuffer();
 		   s.append("当前wifi网络类型不支持Peap认证\n");
 		   result.setInfo(s.toString());
 		   list.add(result);
 		   
 		   result = new Result();
 		   result.setName("隔离测试结果");
 		   s = new StringBuffer();
 	       s.append("成功率：").append(t.userIsolateSuccessRate).append("%");
 		   result.setInfo(s.toString());
 		   list.add(result);
 		   
 		   result = new Result();
 		   result.setName("漫游测试结果");
 		   s = new StringBuffer();
 	       s.append("成功率：").append(t.userRoamSuccessRate).append("%");
 		   result.setInfo(s.toString());
 		   list.add(result);

 		   result = new Result();
 		   result.setName("信号强度测试结果");
 		   s = new StringBuffer();
 	       s.append("CI：").append(t.ci);
 	       s.append("\nLAC：").append(t.lac);
 	       s.append("\n信号强度：").append(t.rssi).append(" dBm");
 		   result.setInfo(s.toString());
 		   list.add(result);
 		   group.setResultList(list);
 		   groupResultlist.add(group);
 	   }
 	   
 	  
     }
   /* private void showTaskNum() {
        int infoSize = ctx.getIntent().getIntExtra("info", 0);
        if (infoSize > 0) {
            DBManager db = new DBManager(ctx);
            List<TaskConfig> configs = db.getTask(task.apName, String.valueOf(task.id));
            db.release();
            StringBuffer s = new StringBuffer();
            s.append("已完成此热点下  ").append(configs.size()).append(" 项测试.");
            s.append("还剩 ").append(infoSize - configs.size()).append(" 项.");
            TextView tv = new TextView(ctx);
            tv.setText(s);
            tv.setTextSize(14);
            tv.setTextColor(Color.CYAN);
            taskNumLayout.addView(tv);
        }
    }*/
    public void showCameraDialog(final Activity ctx) { 
        AlertDialog.Builder builder = new AlertDialog.Builder(ctx);  
        	builder.setTitle("拍照提示");   
        	builder.setMessage("是否进行测试现场拍照？");
        	builder.setCancelable(true);
     		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
     			public void onClick(DialogInterface dialog, int id) {
                	GlobalApp.who_to_CameraWidget = MessageWhat.TaskTestResultAll;
                   CameraWidget cw = new CameraWidget(ctx);
                   ctx.setContentView(cw);
     			}
     		}); 
     		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
             	public void onClick(DialogInterface dialog, int id) {
             		try {
     					Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
     					field.setAccessible(true);
     					field.set(dialog, true);
     				} catch (Exception e) {
     					e.printStackTrace();
     				}
             		dialog.cancel();
             	}
             });
     		wifiListDialog = builder.create();
            wifiListDialog.show();
		  
    }
	public Task getTask() {
		return task;
	}

	public void setTask(Task task) {
		this.task = task;
	}
	public Task dealWebAndPeapData(Task task){
		if(task==null || task.scanResult==null || task.scanResult.SSID==null || !task.scanResult.SSID.equals(WlanManager.SSID_CMCC)){
			task.webDelay = "-1";
			task.webOffSuccessRate="-1";
			task.webSuccessRate = "-1";
		} 
		if(!GlobalApp.isPeapTested) {
			task.peapAuthAvgTime = "-1";
			task.peapDelay="-1";
			task.peapOffSuccessRate = "-1";
			task.peapSuccessRate = "-1";
			task.peapOffCount = "-1";
			task.peapSuccessCount = "-1";
		}
		return task;
	}
}
